import {
  ds
} from "~/tools/data"
import {
  users
} from "~/tools/users"
import {
  pages
} from '~/tools/pages'
import {
  nav2
} from "~/tools/nav2";

Page({
  data: {
    action: 'new',
    index: 0,
    id: '',
    item: {
      showTicket: false
    },
    mod: {},
    errMsg: ""
  },
  async init() {
    const {
      id,
      index
    } = this.data
    const that = this
    ds.coll('module').where({
      _id: ds.cmd.eq(id)
    }).get({
      success: function (res) {
        if (res.data && res.data.length > 0) {
          that.setData({
            mod: res.data[0],
            item: res.data[0].slides[index]
          })
        }
      }
    });

  },
  selImg() {
    wx.navigateTo({
      url: '/pages/common/imgSelect/index'
    })
  },
  addImgClick() {
    this.selImg()
  },
  imgClick() {
    this.selImg()
  },
  imgSelected(files) {
    if (files.length > 0) {
      this.setData({
        'item.img': files[0]
      })
    }
  },
  actionTaped() {
    const {
      item
    } = this.data
    nav2.navigate(`/pages/usercenter/module/action/index?data=${JSON.stringify(item)}`)
  },
  setAction(data) {
    this.setData({
      item: data
    })
  },
  saveClick() {
    const {
      item,
      note
    } = this.data

    pages.back(prePage => {
      prePage.setSlide(item, note)
    })
  },
  backClick() {
    pages.back()
  },
  onLoad(option) {
    const data = this.data
    this.setData({
      item: option.item ? JSON.parse(option.item) : data.item,
      note: option.note ? JSON.parse(option.note) : {}
    })

    this.init();
  },
});